﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.CodeDom.Compiler;
using System.Reflection;
using SharpScriptLib;
namespace SharpScript
{
    class Program
    {
        static List<string> References = new List<string>();
        static List<string> SourceFiles = new List<string>();
        static List<string> Arguments = new List<string>();
        static string MainClass = "";
        static bool PrintH = false;
        static void Main(string[] args)
        {
            PrintAbout();
            if (args.Length < 1)
            {
                PrintHelp();
            }
            else
            {
                ProccessArgs(args);
                Execute();
            }
        }
        static void PrintAbout()
        {
            Console.WriteLine("SharpScript  Copyright (C) 2008  Iraklis Karagkiozoglou");
            Console.WriteLine("This program comes with ABSOLUTELY NO WARRANTY.");
            Console.WriteLine("This is free software, and you are welcome to redistribute it under certain conditions.");
            Console.WriteLine();
        }
        static void PrintHelp()
        {
            Console.WriteLine("SharpScript Usage: sharpscript [options] source files");
            Console.WriteLine("Options:");
            Console.WriteLine("/s \tAdd Source Files");
            Console.WriteLine("/r \tAdd referenced assemblies");
            Console.WriteLine("/rf \tAdd file containing list with reference assemblies");
            Console.WriteLine("/a \tAdd argument to pass");
            Console.WriteLine("/m \tSet the Main Class. Syntax:namespace.class");
            Console.WriteLine("/h \tDisplay the help");
            Console.WriteLine("");
        }
        static void ProccessArgs(string[] args)
        {
            for (int i = 0; i < args.Length; i++)
            {
                if (args[i] == "/r")
                {
                    i++;
                    References.Add(args[i]);

                }
                else if (args[i] == "/rf")
                {
                    i++;
                    if (File.Exists(args[i]))
                    {
                        using (StreamReader file = new StreamReader(args[i]))
                        {
                            string Line = "";
                            while ((Line = file.ReadLine()) != null)
                            {
                                References.Add(Line);
                            }
                        }

                    }
                    else
                    {
                        Console.WriteLine("File {0} does not exists.", args[i++]);
                    }
                }
                else if (args[i] == "/m")
                {
                    i++;
                    MainClass = args[i];

                }
                else if (args[i] == "/h")
                {
                    PrintH = true;
                }
                else if (args[i] == "/a")
                {
                    i++;
                    Arguments.Add(args[i]);

                }
                else if (args[i] == "/s")
                {
                    i++;
                    SourceFiles.Add(args[i]);

                }
            }
        }
        static void Execute()
        {
            if (PrintH)
            {
                PrintHelp();
            }
            if (SourceFiles.Count > 0)
            {
                CompilerParameters Params = new CompilerParameters();
                Params.CompilerOptions = "/t:exe";
                Params.GenerateExecutable = true;
                Params.OutputAssembly = Path.GetTempFileName();
                if (MainClass != "")
                {
                    Params.MainClass = MainClass;
                }
                foreach (string RefAsm in References)
                {
                    Params.ReferencedAssemblies.Add(RefAsm);
                }
                string[] SFiles = new string[SourceFiles.Count];
                for (int i = 0; i < SourceFiles.Count; i++)
                {
                    SFiles[i] = SourceFiles[i];
                }
                string[] NArguments = new string[Arguments.Count];
                for (int i = 0; i < Arguments.Count; i++)
                {
                    NArguments[i] = Arguments[i];
                }
                try
                {
                    Assembly asm = SharpScriptLib.SharpScriptLib.CompileFromFile(Params, SFiles);
                    SharpScriptLib.SharpScriptLib.Run(asm.FullName, Params.OutputAssembly, NArguments);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            else
            {
                Console.WriteLine("No Source files.");
            }
        }
    }
}